<template>
    <div></div>
</template>

<script>
    import Xurl from '../x/xurl';
    import Vue from 'vue';

    export default {
        data(){
            return{
                onlineStateBrowseJsonStr:window.sessionStorage.getItem(Vue.prototype.ONLINESTATEBROWSEMAP),
                onlineStateMap : null
            }
        },
        created() {
            this.onlineStateBrowseJsonStr = this.onlineStateBrowseJsonStr == 'null' ? '' : this.onlineStateBrowseJsonStr;
            // 当前访问令牌
            this. onlineStateMap = eval("(" + this.onlineStateBrowseJsonStr + ")");
            this.initOnlineState()
            setInterval(this.updateOnlineState, 60 * 1000);  // 60秒执行一次
        },
        methods: {
            // 更新浏览记录访问时间
            updateOnlineState() {
                let map = JSON.parse(sessionStorage.getItem(Vue.prototype.ONLINESTATEBROWSEMAP));
                if (map != null && !$.isEmptyObject(map)) {
                    let param = "";
                    for (let key in map) {
                        if (map[key] == null) continue;
                        param = param.concat("&", key, "=", map[key])
                    }
                    param = param.substr(1);
                    let url = this.NW_API_vue + "/consumers/onlineState?".concat(param);
                    Xurl.get(url)
                }
            },
            initOnlineState() {
                if (window.performance.navigation.type != window.performance.navigation.TYPE_RELOAD
                        && window.performance.navigation.type != window.performance.navigation.TYPE_BACK_FORWARD) {
                    // 新加载的页面：保存浏览令牌到本地，发送浏览请求
                    sessionStorage.setItem(Vue.prototype.ONLINESTATEBROWSEMAP, JSON.stringify(this.onlineStateMap));
                }
                this.updateOnlineState();
            },
        }
    }
</script>

<style scoped>

</style>
